package mynote;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

//输入框提示信息显示
public class TextFocusListener implements FocusListener {
    private String tipText;
    private JTextField textField;

    public TextFocusListener(String tipText, JTextField textField) {
        this.tipText = tipText;
        this.textField = textField;
        this.textField.setText(tipText);
        this.textField.setForeground(Color.GRAY);
    }

    @Override
    public void focusGained(FocusEvent e) {
        //有焦点清除提示
        if (this.textField.getText().equals(tipText)) {
            this.textField.setText("");
            this.textField.setForeground(Color.BLACK);
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
        //无焦点显示提示
        if (this.textField.getText().equals("")) {
            this.textField.setText(tipText);
            this.textField.setForeground(Color.GRAY);
        }
    }
}
